<?php
class marketdao extends CI_Model{
	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
	}

	function create($name, $type, $description,
			$url, $email, $currencyType){
		$data = array(
				'market_type_name'	=> $type,
				'market_name'	=> $name,
				'market_description'	=> $description,
				'market_picture_url'	=> $url,
				'market_email'	=> $email,
				'market_currency_name' => $currencyType,
		);

		$str = $this->db->insert_string('market', $data);
		$res = $this->db->query($str);
		if (!$res) {
			return 0;
		} else {
			$return_id = $this->db->insert_id();
			array_push($data,
			array('market_id' => $return_id));
			return $data;
		}

	}
	
	function get_by_address($neigborhood_id){
		if (intval($neigborhood_id) > 0) {
		$this->db->select('market.*,market_has_neighborhood.market_branch_phone');
		$this->db->from('market');
		$this->db->join('market_has_neighborhood', 'market.market_id = market_has_neighborhood.market_id');
		$this->db->where('market_has_neighborhood.neighborhood_id', $neigborhood_id);
		$query = $this->db->get();
		$result = $query->result();
		return $result;
		}	
		return null;
	}

	function del($market_id) {
	 if (intval($market_id) > 0) {
	 	$this->db->where('market_id', $market_id);
	 	$this->db->delete('market');
	 	return ($this->db->affected_rows() == 1) ? TRUE : FALSE;
	 }
	// return FALSE;
	}
	public function getbyid($id){
		$this->db->select('*');
		$this->db->from('market');
		$this->db->where('market_id',$id);
		$query = $this->db->get();
		$market = $query->row();
		return $market;
	}
	
	public function getbyemail($email){
		$this->db->select('*');
		$this->db->from('market');
		$this->db->where('market_email',$email);
		$query = $this->db->get();
		$market = $query->row();
		return $market;
	}
	
	public function getmarkets(){
		$this->db->select('*');
		$this->db->from('market');
		$query = $this->db->get();
		$markets = $query->result();
		return $markets;
	}
	//function add_branch($neigh)

}
?>